package test.adddata;

import cn.hutool.core.collection.ConcurrentHashSet;
import cn.hutool.core.io.FileUtil;
import cn.hutool.db.Db;
import cn.hutool.db.Entity;
import cn.hutool.db.ds.DSFactory;
import com.alibaba.fastjson.JSON;
import lombok.SneakyThrows;
import utils.SmallTool;

import java.util.List;
import java.util.Set;

/**
 * @Author: gcr
 * @Date: 2022-5-25 16:09
 */
public class 排查泰医服务器pmxml表与主表pmid为什么不一样 {
    @SneakyThrows
    public static void main(String[] args) {

        String dbNameMyXml = "spider_pubmed_xml_content";
        String dbNameMy = "spider_pubmed_vip_extend";

        Set<Integer> allPmidXml = new ConcurrentHashSet<>();
        Set<Integer> allPmid = new ConcurrentHashSet<>();

        int breakSize = 100000;
        SmallTool.printMemory();
        addPmid(dbNameMyXml, breakSize, allPmidXml);
        SmallTool.printMemory();
        addPmid(dbNameMy, breakSize, allPmid);

        if (allPmidXml.size() > allPmid.size()) {
            allPmidXml.removeAll(allPmid);
            FileUtil.writeString(JSON.toJSONString(allPmidXml), "/usr/local/gcr/泰医pmxmlallPmidXml主表重复不存在pmid的id集合.log", "UTF-8");

        } else {
            allPmid.removeAll(allPmidXml);
            FileUtil.writeString(JSON.toJSONString(allPmid), "/usr/local/gcr/泰医pmxmlallPmid主表重复pmid的id集合.log", "UTF-8");
        }

    }

    @SneakyThrows
    public static void addPmid(String dbNameMy, Integer breakSize, Set<Integer> allPmid) {
        SmallTool.printMemory();
        SmallTool.printMess("开始表++" + dbNameMy);
        Db use3307 = Db.use(DSFactory.get("taiyi"));
        String allDate = "select max(pmid) as ids  from " + dbNameMy;
        Entity query = use3307.query(allDate).get(0);
        SmallTool.printMess("最大的id 是" + query.getStr("ids"));
        Integer count = query.getInt("ids");
        int pageSize = count / breakSize + 1;
        SmallTool.printMess("全部页码+++++++++" + pageSize);
        SmallTool.printMess("任务开始+++++++++++++++++++++++++++++++++++++++++++++++++++++");
        for (int i = 1; i <= pageSize; i++) {
            int start = breakSize * (i - 1) + 1;
            int end = i * breakSize;
            SmallTool.printMess("开始页码++" + i + "++id++" + start + "结束id++++" + end);
            String meSql = "select pmid from " + dbNameMy + " where pmid between " + start + " and " + end;
            List<Entity> queryMe = use3307.query(meSql);
            queryMe.parallelStream().forEach(pmDeatil -> {
                Integer pmid = pmDeatil.getInt("pmid");
                allPmid.add(pmid);
            });
        }
    }
}
